﻿using ElectronicSelling.BusinessLogic;
using ElectronicSelling.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ElectronicSelling.UserControl
{
    public partial class CartContent : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CreateCart();
                GetProductFromCartSession();

            }
        }

        public void CreateCart()
        {
            DataTable cart = new DataTable();
            if (Session["cart"] == null)
            {
                cart.Columns.Add("ProductId");
                //cart.Columns.Add("Name");
                cart.Columns.Add("Quantity");
                Session["cart"] = cart;
            }
            else
            {
                cart = Session["cart"] as DataTable;
            }
            if (Request.QueryString["cartAction"] != null)
            {
                string action = Request.QueryString["cartAction"];
                switch (action)
                {
                    case "add":
                        if (Request.QueryString["pid"] != null)
                        {
                            string productId = Request.QueryString["pid"];
                            bool isExisted = false;
                            foreach (DataRow dr in cart.Rows)
                            {
                                if (dr["ProductId"].ToString() == productId)
                                {
                                    dr["Quantity"] = int.Parse(dr["Quantity"].ToString()) + 1;
                                    isExisted = true;
                                    break;
                                }
                            }
                            if (!isExisted)
                            {
                                DataRow dr = cart.NewRow();
                                dr["ProductId"] = productId;
                                dr["Quantity"] = 1;
                                cart.Rows.Add(dr);
                            }

                            Session["cart"] = cart;
                        }
                        break;
                }
            }
        }

        public void GetProductFromCartSession()
        {
            if (Session["cart"] != null)
            {
                try
                {
                    DataTable cart = Session["cart"] as DataTable;
                    if (cart.Rows.Count == 0)
                    {
                        VisibleControl();
                    }
                    else
                    {
                        DataTable dt = new DataTable();
                        dt = CartLogic.GetCartFromSession(cart);
                        EnableControl();
                        rptCartDetail.DataSource = dt;
                        rptCartDetail.DataBind();
                    }
                }
                catch (Exception)
                {
                    Response.Redirect(Directory.Error);
                }
            }
            else
            {
                Response.Redirect("ProductList.aspx");
            }
        }

        public double GetTotalOrder()
        {
            DataTable cart = Session["cart"] as DataTable;
            return CartLogic.GetTotalDouble(cart);
        }

        protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string cmdName = e.CommandName;
            string productId = e.CommandArgument.ToString();
            switch (cmdName)
            {
                case "Update":
                    Label lblProductId;
                    for (int i = 0; i < rptCartDetail.Items.Count; i++)
                    {
                        lblProductId = (Label)rptCartDetail.Items[i].FindControl("lblProductId");
                        if (lblProductId.Text.Trim().Equals(productId))
                        {
                            TextBox txtQuantity = (TextBox)rptCartDetail.Items[i].FindControl("txtQuantity");
                            string quantity = txtQuantity.Text.Trim();
                            CartLogic.CartUpdate(Session["cart"] as DataTable, productId, quantity);
                        }
                    }
                    break;
                case "Remove":
                    DataTable cart = Session["cart"] as DataTable;
                    for (int i = 0; i < cart.Rows.Count; i++)
                    {
                        if (cart.Rows[i]["ProductId"].ToString().Equals(productId))
                        {
                            cart.Rows.RemoveAt(i);
                        }
                    }
                    break;
            }
            //GetProductFromCartSession();
            Response.Redirect("Cart.aspx");
        }

        protected void btnControl_Click(object sender, EventArgs e)
        {
            switch ((sender as Button).ID)
            {
                case "btnBuyContinue":
                    Response.Redirect(Directory.Product_List_Page);
                    break;
                case "btnPayment":
                    break;
                case "btnSendOrder":
                    Response.Redirect(Directory.Order_Product_Page);
                    break;
                case "btnDeleteCart":
                    Session.Remove("cart");
                    Response.Redirect("Cart.aspx");
                    break;
            }
        }

        protected void VisibleControl()
        {
            btnBuyContinue.Visible = false;
            btnDeleteCart.Visible = false;
            btnPayment.Visible = false;
            btnSendOrder.Visible = false;
            btnDeleteCart.Visible = false;
        }

        protected void EnableControl()
        {
            btnBuyContinue.Visible = true;
            btnDeleteCart.Visible = true;
            btnPayment.Visible = true;
            btnSendOrder.Visible = true;
            btnDeleteCart.Visible = true;
        }

    }
}